// pages/gallery/order/list/index.js
Page({
  data: {
    type: '',
    uuid: '',
    orderlist: []
  },
  onPullDownRefresh () {
    this.getNewList()
  },
  onLoad: function (options) {
    // let type = options.type
    let uuid = tt.getStorageSync('gallery__uuid')
    this.setData({uuid})
    console.log(this.getNewList)
    this.getNewList()
  },
  getNewList () {
    tt.request({
      url: 'https://douyinmini.163ar.cn/api/order/list/gallery?num=100000000&uuid=' + this.data.uuid + '&type=' + this.data.type, // 目标服务器url
      success: (res) => {
        tt.stopPullDownRefresh()
        if (res.data.code) {
          tt.showToast({
            title: '系统异常'
          });
        } else {
          let orderlist = res.data.data.list.map(item => {
            return {
              ...item,
              order_price: parseFloat(item.price) + parseFloat(item.freight),
              order_type: this.renderOrderStatus(item)
            }
          })
          console.log(orderlist)
          this.setData({
            orderlist: orderlist
          })
        }
      },
      fail: (err) => {
        tt.stopPullDownRefresh()
        console.log(err)
      }
    });
  },
  renderOrderStatus (order) {
    if (order.order_status === 0) {
      return {
        text: '已取消', color: '#eeeeee', type: 5
      }
    }
    if (order.pay_status === 0) {
      return {
        text: '待付款', color: '#ff0000', type: 1
      }
    }
    if (order.delivery_status === 0) {
      return {
        text: '待发货', color: '#ff52aa', type: 2
      }
    }
    if (order.receipt_status === 0) {
      return {
        text: '待收货', color: '#54ff68', type: 3
      }
    }
    return {
      text: '订单完成', color: '#a6e22e', type: 4
    }
  },
  orderDelivery () {
    tt.showLoading({
      title: '正在催促'
    })
    setTimeout(() => {
      tt.hideLoading()
      tt.showToast({
        title: '催单成功'
      })
    }, 500)
  },
  enterReceipt (e) {
    let item = e.currentTarget.dataset.item
    console.log(item)
    tt.request({
      url: 'https://douyinmini.163ar.cn/api/order/active',
      method: 'POST',
      data: {
        user_id: item.user_id,
        order_id: item._id,
        active: 'enter_receipt' // 确认收货
      },
      success: (res) => {
        if (res.data.code) {
          tt.showToast({
            title: res.data.msg, icon: 'none'
          })
          return
        }
        tt.startPullDownRefresh();
      }
    })
  },
  changeTabs (e) {
    let type = e.currentTarget.dataset.type
    if (this.data.type == type) {
      return
    }
    this.setData({type})
    tt.startPullDownRefresh();
  },
  navto (e) {
    let url = e.currentTarget.dataset.url
    tt.reLaunch({
      url: url
    });
  }
})